package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.commen.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    //添加角色
    @Override
    public void add(Role role) {
        //补全基础属性
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        //调用mapper,执行添加
        roleMapper.insert(role);
    }

    //查询角色列表
    @Override
    public PageResult<Role> list(String name, String label, Integer page, Integer pageSize) {
        ////配置pageHelper分页参数
        PageHelper.startPage(page,pageSize);
        List<Role> roleList = roleMapper.selectList(name,label);
        Page<Role> pageInfo=(Page<Role>) roleList;
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getResult());
    }

    //根据id查询角色

    @Override
    public Role getById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void update(Role role) {
        //补全基础信息
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public void delete(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public List<Role> getAll() {
        return roleMapper.getAll();
    }
}
